//
//  XSQTabViewController.swift
//  Hpyerglycemic
//
//  Created by  on 2017/12/6.
//  Copyright © 2017年 Anna. All rights reserved.
//

import UIKit
class XSQTabBarController: UITabBarController, UITabBarControllerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Close translucent
        self.tabBar.isTranslucent = false
        self.tabBar.barTintColor = UIColor.white
        self.tabBar.tintColor = XSQ_ColorBlue
        if #available(iOS 15, *) {
            let appearance = UITabBarAppearance.init()
            appearance.backgroundColor = UIColor.white
           
            let selTitleAttr = [
                NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12),
                NSAttributedString.Key.foregroundColor: XSQ_ColorBlue
            ]
            appearance.stackedLayoutAppearance.selected.titleTextAttributes = selTitleAttr // 设置选中attributes
            self.tabBar.scrollEdgeAppearance = appearance
            self.tabBar.standardAppearance = appearance
        }
        addChildViewControllers()
        selectedIndex = 0
        self.delegate = self
    }
    
    // MARK: - Add childController
    private func addChildViewControllers() {
        addChildViewController(childController: DeviceHomeViewController(), title: "设备", imageName: "tabbar_device")
        addChildViewController(childController: MaterialHomeViewController(), title: "素材", imageName: "tabbar_materials")
        addChildViewController(childController: MineHomeViewController(), title: "我的", imageName: "tabbar_mine")
    }
    
    // MARK: - Init childController
    private func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        childController.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_s")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        childController.title = title
        let XSQNav = XSQNavigationController(rootViewController: childController)
        self.addChild(XSQNav)
    }
    
    // MARK: - Memory warning
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        XPLog(message: viewController)
        if let vc:XSQNavigationController = viewController as? XSQNavigationController {
//            if let homeVc = vc.topViewController as? StatisticsHomeViewController{
//                XPLog(message: homeVc.isViewLoaded)
//                if homeVc.isViewLoaded == true{
//                    homeVc.approveVC?.loadReviewRecordList()
//                    homeVc.bookVC?.loadBookList()
//                }
//            }
            
        }
    }
    
}
